package ejbPart.session.stateless.testAccount;

import ejbPart.entity.bankAccount.*;
import exceptions.AccountException;
import exceptions.DataAccessException;
import exceptions.LookupException;

import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;

public class TestAccountBean implements SessionBean {

        /*--------------- Business method section ---------------------*/

    public double checkBalance() throws DataAccessException {
        try {
            AccountLocal account = lookupEntityBean();
            return account.getBalance();
        } catch (LookupException e) {
            throw new DataAccessException(e);
        }
    }

    public void addToAccount(double amount) throws DataAccessException {
        try {
            lookupEntityBean().deposite(amount);
        } catch (AccountException e) {
            throw new DataAccessException(e);
        } catch (LookupException e) {
            throw new DataAccessException(e);
        }
    }

    public Account findAccountById(String id) throws DataAccessException {
        try {
            Context ctx = new InitialContext();
            final String path = "java:comp/env/beans/entity/AccountRemote";
            Object obj = ctx.lookup(path);
            AccountHome accountHome = (AccountHome) PortableRemoteObject.narrow(obj, AccountHome.class);
            return accountHome.findByPrimaryKey(new AccountPK(id));
        } catch (NamingException e) {
            throw new DataAccessException(e);
        } catch (RemoteException e) {
            throw new DataAccessException(e);
        } catch (FinderException e) {
            throw new DataAccessException(e);
        }
    }



    private AccountLocal lookupEntityBean() throws LookupException, DataAccessException {
        try {
            Context ctx = new InitialContext();
            final String path = "java:comp/env/beans/entity/Account";
            Object obj = ctx.lookup(path);
            AccountLocalHome accountHome = (AccountLocalHome) PortableRemoteObject.narrow(obj, AccountLocalHome.class);

            final String key = "123-456-7890";
            return accountHome.findByPrimaryKey(new AccountPK(key));
        } catch (NamingException e) {
            throw new LookupException("Can't lookup account entity bean", e);
        } catch (FinderException e) {
            throw new DataAccessException("Can't find entity bean with given primary key", e);
        }
    }


    public void ejbCreate() throws CreateException {
        System.out.println("-----Session bean has been created");
    }

    @Override
    public void setSessionContext(SessionContext sessionContext) throws EJBException {
    }

    @Override
    public void ejbRemove() throws EJBException {
        System.out.println("------Session bean removed");
    }

    @Override
    public void ejbActivate() throws EJBException {
        System.out.println("------Session bean activated");
    }

    @Override
    public void ejbPassivate() throws EJBException {
        System.out.println("------Session bean passivated");
    }
}
